// pages/product/product.js
import {Product} from 'product-model.js';
import {Cart} from '../cart/cart-model.js';
var product = new Product();
var cart = new Cart();
Page({

  /**
   * 页面的初始数据
   */
  data: {
     id:null,
     countsArray:[1,2,3,4,5,6,7,8,9,10],
     productCounts:1,
     currentTabsIndex: 0,
    loadingHidden:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id;
    this.data.id = id;
    this._loadData();
  },

  _loadData:function(){
    console.log(cart.getCartTotalCounts());
    var that = this;
    product.getDetailInfo(this.data.id,(data)=>{
      that.setData({
        'product':data,
        'cartTotalCounts':cart.getCartTotalCounts(),
        loadingHidden: true
      })
    })
  },
  bindPickerChange:function(event){
    var index = event.detail.value;
    var selecteedCount = this.data.countsArray[index];
    this.setData({
      productCounts:selecteedCount
    })
  },
  onTabsItemTap:function(event){
    var index = product.getDataSet(event,'index');
    this.setData({
      currentTabsIndex:index
    })
  },
  onAddingToCartTap:function(){
    var tempObj = {}, keys = ['id', 'name', 'main_img_url', 'price'];
    for (var key in this.data.product) {
      if (keys.indexOf(key) >= 0) {
        tempObj[key] = this.data.product[key];
      }
    }
    console.log(tempObj);
    cart.add(tempObj, this.data.productCounts);
    var counts = this.data.cartTotalCounts + this.data.productCounts;
    this.setData({
      'cartTotalCounts':counts
    })
  },
  onCartTap:function(event){
    wx.switchTab({
      url: '/pages/cart/cart'
    });
  }
  


})